package com.sk.bilibili.adapter.simple;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.sk.bilibili.databinding.ActivitySpaceSearchBinding;
import com.sk.bilibili.databinding.SearchVideoSetItemBinding;
import com.sk.bilibili.dto.GenericInfo;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.utils.GenericUtils;
import com.sk.bilibili.viewHolder.BaseViewHolder;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import lombok.SneakyThrows;

public abstract class BaseAdapter<V extends ViewBinding,D> extends android.widget.BaseAdapter {

    protected List<D> list;
    protected Context context;

    public BaseAdapter(Context context) {
        this.context = context;
        list=new ArrayList<>();
    }

    public void setData(List<D> data){
        list.clear();
        addData(data);
    }

    public void addData(List<D> data) {
        list.addAll(data);
        notifyDataSetChanged();
    }

    @SneakyThrows
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        V bind;
        if(convertView==null){
            GenericInfo<V, BaseModel> parse = GenericUtils.parse(getClass());
            Class<V> bindClass = parse.getBindClass();
            Method method = bindClass.getDeclaredMethod("inflate", LayoutInflater.class,ViewGroup.class,boolean.class);
            bind = (V) method.invoke(null, LayoutInflater.from(context),parent,false);
            convertView=bind.getRoot();
            convertView.setTag(bind);
        }else {
            bind= (V) convertView.getTag();
        }
        bindView(bind,position);
        return convertView;
    }

    protected abstract void bindView(V bind, int position);

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}